package isDim;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.Lock;

public class dimUpdateConfirmationHandler implements Runnable {
	
	private dimUpdateId lastUpdate;
	private Lock lock;
	private BlockingQueue<dimCargo> box;
	
	public dimUpdateConfirmationHandler(dimUpdateId lu, Lock l, BlockingQueue<dimCargo> b)
	{
		lastUpdate = lu;
		lock = l;
		box = b;
	}
		
	public void run()
	{
		while(true)
		{
			try{
				Thread.sleep(10*1000);
			}catch(Exception e){ System.out.print("FATAL ERROR. Error trying to sleep dimUpdateConfirmationHandler thread: " + e.getMessage() + "\n"); System.exit(666); }
			
			try{
				lock.lock();
				if (lastUpdate.ok() == false)
					box.put(new dimCargo(dimCargo.Type.FORCE_UPDATE));
				lock.unlock();
			}catch(Exception e){ System.out.println("Error forcing update: " + e.getMessage()); if (lock.tryLock()) lock.unlock(); }
		}
	}

}
